﻿namespace BEGDC.CC.Web.UserControls
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

	/// <summary>
	///		Summary description for PageNavigation.
	/// </summary>
	public class PageNavigation : System.Web.UI.UserControl
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here

			if (this._Navigation == string.Empty)
				this.GetNavigation();
			try
			{
				if (((BEGDC.CC.BL.Workflow.CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id == string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>window.parent.location='../login.aspx'</script>");
				}
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>window.parent.location='../login.aspx'</script>");
			}
		}

		#region Web Form Designer generated code

		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}

		#endregion

		private void GetNavigation()
		{
			if (System.Web.HttpContext.Current.Cache["WebSiteMap"] == null)
			{
				this.ReadWebSiteMap();
				//return;
			}
			if (System.Web.HttpContext.Current.Cache["WebSiteMap"] == null)
			{
				return;
			}
			BEGDC.CC.BL.Common.SiteMapNodeCollection sitemap = System.Web.HttpContext.Current.Cache["WebSiteMap"] as BEGDC.CC.BL.Common.SiteMapNodeCollection;

			string filename = System.IO.Path.GetFileName(Page.Request.FilePath);

			BEGDC.CC.BL.Common.SiteMapNode node;

			if (Page.Request.Url.Query.IndexOf("?") >= 0)
			{
				if (Page.Request.Url.Query.IndexOf("&") >= 0)
				{
					node = sitemap.FindNode(filename + Page.Request.Url.Query.Substring(0, Page.Request.Url.Query.IndexOf("&")));
					if (node == null)
					{
						node = sitemap.FindNode(filename);
						if (node == null)
							return;
					}
				}
				else
				{
					node = sitemap.FindNode(filename + Page.Request.Url.Query.Substring(0));
					if (node == null)
					{
						node = sitemap.FindNode(filename);
						if (node == null)
							return;
					}
				}
			}
			else
			{
                node = sitemap.FindNodeByPath(Page.Request.FilePath);

                if (node == null)
                {
                    node = sitemap.FindNode(filename);

                    if (node == null)
                    {
                        node = sitemap.FindNode(filename + Page.Request.Url.Query);
                        if (node == null)
                            return;
                    }
                }
			}
			string navigation = string.Empty;
			while(node != null)
			{
                navigation = "&nbsp;&raquo;&nbsp;" + node.Title + " " + navigation;
				node = node.ParentNode;
			}

//			this.lblNavigation.Text = navigation;
			this._Navigation = navigation;
		}

		protected System.Web.UI.WebControls.ImageButton btnHelp;
		protected System.Web.UI.WebControls.ImageButton btnExit;

		private string _Navigation = string.Empty;

		[Browsable(true)]
		[Category("Appearance")]
		[Description("Page Navigation")]
		[DefaultValue("")]
		public string Navigation
		{
			get
			{
				return this._Navigation;
			}
			set
			{
				this._Navigation = value;
			}
		}

		protected void ReadWebSiteMap()
		{
			string filename = this.Request.PhysicalApplicationPath + "\\websitemap.xml";
			if (System.IO.File.Exists(filename))
			{
				System.IO.TextReader tr = System.IO.File.OpenText(filename);
				string xml = tr.ReadToEnd();

				tr.Close();
				BEGDC.CC.BL.Common.SiteMapNodeCollection sitemap = new BEGDC.CC.BL.Common.SiteMapNodeCollection(xml);

				System.Web.HttpContext.Current.Cache.Add("WebSiteMap", sitemap,null,System.Web.Caching.Cache.NoAbsoluteExpiration ,TimeSpan.FromSeconds(30), System.Web.Caching.CacheItemPriority.Low, null);
			}
		}

		public string LastLoginTime
		{
			get
			{
				if (Page.Request.Cookies["LastLoginTime"] == null)
					return string.Empty;
				else
					return Page.Request.Cookies["LastLoginTime"].Value.ToString();
			}
		}
	}
}